package com.web;

import com.pojo.User;
import com.service.impl.userServiceImpl;
import com.service.userService;
import com.utils.webUtils;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class userServlet extends action{
    public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        User user = new User();
        webUtils.copyParameTobean(user,req.getParameterMap());
      /*  String username=req.getParameter("username");
        String password=req.getParameter("password");*/
        userService userservice = new userServiceImpl();
        user = userservice.logUser(user);
        if(user!=null){
            req.getRequestDispatcher("/项目/pages/user/login_success.jsp").forward(req,resp);
        }else{
            req.setAttribute("msg","用户名或密码错误");
            req.getRequestDispatcher("/项目/pages/user/login.jsp").forward(req,resp);
        }

    }
    public void regist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        User user = new User();
        webUtils.copyParameTobean(user,req.getParameterMap());
       /* String username=req.getParameter("username");
        String password= req.getParameter("password");
        String email=req.getParameter("email");
        String repwd=req.getParameter("repwd");
        //密码确认密码是否匹配
        if(!password.equals(repwd)){
            req.getRequestDispatcher("/项目/pages/user/regist.jsp").forward(req,resp);
        }*/
        userService userservice = new userServiceImpl();
        //检查用户名是否存在
        if(userservice.existsUserName(user.getUsername())){
            //存在 (不可注册)
            req.setAttribute("msg","用户名存在不可注册!!!!");
            req.getRequestDispatcher("/项目/pages/user/regist.jsp").forward(req,resp);
        }
        userservice.register(user);
        req.getRequestDispatcher("/项目/pages/user/regist_success.jsp").forward(req,resp);
    }
}
